package kr.hyunist.androidcomplete01saeildamil.ch19;

import kr.hyunist.androidcomplete01saeildamil.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

public class ThreadHadlerEx05 extends Activity {
	int mMainValue = 0;
	TextView mMainText;
	TextView mBackText;
	BackThread th;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.threadtest01);

		mMainText = (TextView) findViewById(R.id.threadtesttv1);
		mBackText = (TextView) findViewById(R.id.threadtesttv2);

		th = new BackThread(h);
		th.setDaemon(true);
		th.start();
	}

	Handler h = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 0) {
				mBackText.setText("BackText : " + msg.arg1);
			}
		}

	};

	public void mOnClick(View v) {
		mMainValue++;
		mMainText.setText("MainValue : " + mMainValue);
	}

}

class BackThread extends Thread {
	int mBackValue = 0;
	Handler h;

	public BackThread(Handler handler) {
		this.h = handler;
	}

	@Override
	public void run() {
		while (true) {
			mBackValue++;
			Message msg = new Message();
			msg.what = 0;
			msg.arg1 = mBackValue;
			h.sendMessage(msg);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {

			}
		}
	}

}